<template>
  <div class="prcList">
    <Loading v-if="loading"></Loading>
    <div class="info orderBox orderInfo" id="div" v-else>
      <table class="listInfo mt20">
        <colgroup >
            <col width="6%">
            <col width="4%">
            <col width="18%">
            <col width="4%">
            <col width="4%">
            <col width="4%">
            <col width="4%">
            <col width="4%">
            <col width="4%">
            <col width="4%">
            <col width="4%">
            <col width="4%">
            <col width="4%">
            <col width="4%">
        </colgroup>
        <thead>
          <tr>
            <td>时间</td>
            <td>订单编号</td>
            <td>产品</td>
            <td>产品编号</td>
            <td>数量</td>
            <td>折后价</td>
            <td>订单信息</td>
            <td>操作</td>
          </tr>
        </thead>
        <tbody>
          <tr class="orderList" >
            <td>{{date.created_at}}</td>
            <td>{{date.trade_no}}</td>
            <td>
              <p v-for="list in date.order_items">{{list.goods_name}}</p>
            </td>
            <td>
              <p style="text-align: center;" v-for="list in date.order_items">{{list.goods_sn}}</p>
            </td>
            <td>
              <p style="text-align: center;" v-for="list in date.order_items">{{list.goods_count}}</p>
            </td>
            <td>{{date|aa}}</td>
            <td v-if="date.user_address">
              {{date.user_address.post_code}},
              {{date.user_address.address}},
              {{date.user_address.first_name}},
              {{date.user_address.mobile}}
            </td>
            <td><span class="print" @click="kk">打印</span></td>
          </tr>
        </tbody>
        
      </table>

  </div>
  </div>
</template>
<script>
import Vue from 'vue'
import Loading from '@/components/loading'
import axios from 'axios'
import { Get, Post ,Put } from '@/config/api'
export default {
  name: 'orderPrint',
  data() {
    return {
      loading: true,
      date:'',
      tableData: [],
      tableNum: 800, //每页显示多少条
      tableCont: 0, //一共有多少条信息
      tablePage: 1, //当前页码
      time1:'',//选择开始时间
      time2:'',//选择结束时间
      number:''//订单数
    }
  },
  filters:{
      state(vule){
        if(vule=='1'){
          return '未处理'
        }else if(vule=='2'){
          return '送货中'
        }else if(vule=='3'){
          return '已完成'
        }else if(vule=='4'){
          return '已取消'
        }
      },
      type(vule){
        if(vule=='1'){
          return '中通'
        }else if(vule=='2'){
          return '圆通'
        }else if(vule=='3'){
          return '顺风'
        }
      },
      allget(val){
        let len=val.length
        let all=0;
        for(let i=0;i<len;i++){
          all+=val[i].goods_count*val[i].market_price
        }
        return all.toFixed(2); 
      },
      allpost(val){
        let len=val.length
        let all=0;
        for(let i=0;i<len;i++){
          all+=val[i].goods_count*val[i].goods_price
        }
        return all.toFixed(2); 
      },
      //优惠后多少钱
      aa(val){
        let len=val.order_items.length
        let all=0;
        for(let i=0;i<len;i++){
          all+=val.order_items[i].goods_count*val.order_items[i].goods_price*val.ratio/100
        }
        return all.toFixed(2); 
      },
      //单利润
      bb(val){
        let ka=0;
        ka=parseFloat(val.goods_price)-parseFloat(val.market_price)
        let num = ka.toFixed(2); 
        return num
      },
      //总利润
      cc(val){
        let len=val.order_items.length
        let all=0;
        let ka=0;
        let kb=0;
        let date=val.order_items
        for(let i=0;i<len;i++){
          ka+=parseFloat(date[i].goods_price*date[i].goods_count)
        }
        for(let i=0;i<len;i++){
          kb+=parseFloat(date[i].market_price*date[i].goods_count)
        }
        all=(ka*val.ratio/100)-kb-2
        let num = all.toFixed(2); 
        return num
      }
      //所以订单利润
  },
  computed:{
    //总利润
    allPrice(){
      let len=this.tableData.length
      let date=this.tableData
      let kb=0
      for(let i=0;i<len;i++){
        let kdate=date[i].order_items
        let klen=date[i].order_items.length
        let jiage=date[i].order_amount
        let ka=0
        for(let j=0;j<klen;j++){
          ka+=kdate[j].market_price*kdate[j].goods_count
        }
        let kk=jiage-ka-2
        console.log(kk)
        kb+=kk
      }
      return kb.toFixed(2)
    },
    //营业额
    allA(){
      let len=this.tableData.length
      let date=this.tableData
      let kb=0
      for(let i=0;i<len;i++){
        let jiage=date[i].order_amount
        kb+=jiage
      }
      return kb.toFixed(2)
    },
    //总收入
    allB(){
      let len=this.tableData.length
      let date=this.tableData
      let kb=0
      for(let i=0;i<len;i++){
        let jiage=date[i].order_amount-2
        kb+=jiage
      }
      return kb.toFixed(2)
    }
  },
  mounted() {
    this.getData()
  },
  components: {
    Loading
  },
  methods: {
    //获取数据
    async getData() {
      let reg=await Get('/order/'+this.$route.query.id+'?_include=user,order_items,user_address,pay_type',this.$store.state.token)
      console.log(reg)
      this.date=reg.data;
      this.loading=false
    },
    kk(){
      try {   
          var Wsh=new ActiveXObject("WScript.Shell");   
          HKEY_Key="header";   
          Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");   
          HKEY_Key="footer";   
          Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");   
      }catch(e){}
      printme();  
    }
  }
}

function printme() 
{ document.body.innerHTML=document.getElementById('div').innerHTML 
  window.print(); 
} 


var HKEY_Root,HKEY_Path,HKEY_Key;   
HKEY_Root="HKEY_CURRENT_USER";   
HKEY_Path="\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";   
 

</script>

